﻿Shader "Unlit/002"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1,1,1,1)
        _Specular("Specular", Color) = (1,1,1,1)
        _Gloss("Gloss", Range(1,256)) = 5
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            
            //只有在CGROGRAM内再次定义一个与属性块内名字与类型相同的变量，属性块对应的变量才能起作用
           fixed4 _Diffuse;
           fixed4 _Specular;
           float _Gloss;
         
            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 color:COLOR0;
            };

         
            v2f vert (appdata_full v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLight));
                
                fixed3 reflectLightDir = normalize(reflect(-worldLight, worldNormal));
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - UnityObjectToWorldDir(v.vertex).xyz);
                
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(reflectLightDir,viewDir)),_Gloss);
                o.color = diffuse + ambient + specular;
                
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 color = fixed4(i.color, 1);
                return color;
            }
            ENDCG
        }
    }
}
